package tcu.iot.gasmanage.mapper.provider;

import tcu.iot.gasmanage.entity.person.PersonQuery;

public class PersonProvider {
    public String findPerson(PersonQuery personQuery) {
        StringBuffer sql = new StringBuffer("Select * from person where 1=1 ");

        CreateSql(personQuery, sql);

        if (personQuery.getSort().equals("-id"))
            sql.append("order by id DESC ");
        if (personQuery.getSort().equals("+id"))
            sql.append("order by id ASC ");

        return sql.toString();
    }

    public String SetPersonOrder(PersonQuery personQuery) {
        StringBuffer sql = new StringBuffer("Select * from person ");

        if (personQuery.getSort().equals("-id"))
            sql.append("order by id DESC ");
        if (personQuery.getSort().equals("+id"))
            sql.append("order by id ASC ");

        return sql.toString();
    }

    public String findCountPerson(PersonQuery personQuery) {
        StringBuffer sql = new StringBuffer("Select count(*) from person where 1=1 ");

        CreateSql(personQuery, sql);

        return sql.toString();
    }

    private void CreateSql(PersonQuery personQuery, StringBuffer sql) {
        if (!personQuery.getUsername().equals("")) {
            sql.append("and username='").append(personQuery.getUsername()).append("' ");
        }
        if (!personQuery.getPipeline().equals("")) {
            sql.append("and pipeline='").append(personQuery.getPipeline()).append("' ");
        }
        if (!personQuery.getManagement().equals("")) {
            sql.append("and management='").append(personQuery.getManagement()).append("' ");
        }
        if (!personQuery.getSite().equals("")) {
            sql.append("and site='").append(personQuery.getSite()).append("' ");
        }
        if (!personQuery.getRegion().equals("")) {
            sql.append("and region='").append(personQuery.getRegion()).append("' ");
        }
    }
}
